﻿using System;
using DosTexPrj.Core.Models;
using DosTexPrj.Core.RepositoriesInterface;
using NUnit.Framework;

namespace DosTexPrj.Implement.NHibernate.Test.Repositories
{
    [TestFixture]
    public class TestUserRepository : RepositoryFixtureBase
    {
        private IUserRepository _userRepository;
        private IRoleRepository _roleRepository;

        protected override void Context()
        {
            this._userRepository = MyServiceLocator.DoGetInstance<IUserRepository>();
            this._roleRepository = MyServiceLocator.DoGetInstance<IRoleRepository>();
        }

        [Test]
        public void Can_Create_User()
        {
            var role = new Role { RoleName = "Administrator" };
            var user = new User
            {
                FirstName = "Kim",
                LastName = "Kim",
                Username = "Kim",
                Password = "Kim",
                Role = role
            };
            role.UsersOfRole.Add(user);
            role = this._roleRepository.Save<Role>(role);
            Assert.IsTrue(user != null && !user.Id.Equals(Guid.Empty) && user.Role == role);
        }

        [Test]
        public void Can_Create_User_With_Existed_Role()
        {
            var role = this._roleRepository.FindByKey<Role>(1);
            var user = new User
            {
                FirstName = "Kim",
                LastName = "Kim",
                Username = "Kim",
                Password = "Kim",
                Role = role
            };
            role.UsersOfRole.Add(user);
            role = this._roleRepository.Save<Role>(role);
            Assert.IsTrue(user != null && !user.Id.Equals(Guid.Empty) && user.Role == role);
        }
    }
}